import axios, {AxiosInstance, AxiosError, AxiosResponse, AxiosRequestConfig} from 'axios';
import {ElMessage} from "element-plus";
import router from "@/router";

axios.defaults.withCredentials = true
const service:AxiosInstance = axios.create({
    withCredentials: true,
    baseURL: 'http://localhost:8083/',
    timeout: 5000
});

service.interceptors.request.use(
    (config: AxiosRequestConfig) => {
        return config;
    },
    (error: AxiosError) => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    (response: AxiosResponse) => {
        if (response.status === 200&&(response.data.code===200||response.data.type==='application/pdf')) {
            return response.data;
        } else if(response.status === 200&&response.data.code===100){
            ElMessage.warning(response.data.msg);
            router.push("login")
        }else{
            ElMessage.warning(response.data.msg);
        }
    },
    (error: AxiosError) => {
        console.log(error);
        return Promise.reject();
    }
);

export default service;
